<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
<title>库存期初建账管理</title>
<meta name="decorator" content="default" />
<link href="${ctxStatic}/bootstrap-editable/css/bootstrap-editable.css" rel="stylesheet" />
<link href="${ctxStatic}/bootstrap-table/bootstrap-table.min.css" rel="stylesheet" />
<script src="${ctxStatic}/bootstrap-editable/js/bootstrap-editable.js"></script>
<script src="${ctxStatic}/bootstrap-table/bootstrap-table.js"></script>
<script src="${ctxStatic}/bootstrap-table/locale/bootstrap-table-zh-CN.js"></script>
<script src="${ctxStatic}/bootstrap-table/extensions/editable/bootstrap-table-editable.js"></script>
<script src="${ctxStatic}/common/format/formatOrderData.js"></script>
<script type="text/javascript">
$(function() {
	$('#tab').bootstrapTable({
//     	toolbar: "#toolbar",
    	clickToSelect :true,
    	uniqueId: "batchnoId",
        columns: [{
        	field:"选择",
        	checkbox: true
        }, {
            field: 'batchnoId',
            title: 'batchnoId'
            , visible: false
        }, {
            field: 'batchType',
            title: '类型',
            formatter:formatBatchType
        }, {
            field: 'vaccineName',
            title: '疫苗名称/注射器类型'
        }, {
            field: 'cType',
            title: '属性',
            width : '150px',
            editable: {
                type: 'select2',
                inputclass:'input-mini',
                mode:'inline',
                source: function (a,b,c,d) {
                    var list = JSON.parse('${fns:getDictListJson("bs_vaccine_type")}');
                    var result = [];
                    for (var i in list) {
                    	result.push({
                    		text : list[i].label,
                    		value : list[i].value
                    	});
                    }
                    return result;
                }
            }
        }, {
            field: 'companyName',
            title: '生产企业'
        }, {
            field: 'companyCode',
            title: '生产企业编码',
            visible:false 
        },{
            field: 'batchno',
            title: '批号',
            formatter:formatBatchno
        }, {
            field: 'spec',
            title: '规格(剂/支或粒)',
            formatter:formatSpec
        }, {
            field: 'dose',
            title: '剂量'
        }, {
            field: 'vaccExpDate',
            title: '有效日期',
            formatter:formatDate
        }, {
            field: 'storenum',
            title: '数量(支/粒)',
            width:'150px',
            editable: {
                type: 'text',
                title: '建账数量',
                emptytext : '0',
                inputclass:'input-mini',
                mode:'inline',
                validate: function (v) {
                	var num = parseInt(v)
    				var num_reg = /^\+?[1-9][0-9]*$/;
    				if (!num_reg.test(v)) {
    					return '数量不合法';
    				}
                }
            }
        }, {
            field: 'orignprice',
            title: '成本价',
            width:'150px',
            editable: {
                type: 'text',
                emptytext : '0.0',
                inputclass:'input-mini',
                mode:'inline',
                validate: function (v) {
    				var sellprice_reg = /^\d+(\.\d+)?$/;
    				if (!sellprice_reg.test(v)) {
    					return '价格不合法';
    				}
                }
            }
        }, {
            field: 'sellprice',
            title: '出库价',
            width:'150px',
            editable: {
                type: 'text',
                emptytext : '0.0',
                inputclass:'input-mini',
                mode:'inline',
                validate: function (v) {
    				var sellprice_reg = /^\d+(\.\d+)?$/;
    				if (!sellprice_reg.test(v)) {
    					return '价格不合法';
    				}
                }
            }
        }],
        onEditableSave: function (field, row, oldValue, $el) {
			if (field =='cType'){
				if (row.cType == "1"){
	        		row.orignprice = 0;
	        		row.sellprice = 0;
				}else{
	        		row.orignprice = '';
	        		row.sellprice = '';
				}
        		$('#tab').bootstrapTable('updateByUniqueId', { id: row.batchnoId, 'row':row});
        	}
			if(field == 'orignprice'){
                if(row.orignprice == '0'){
                	row.cType = '1';
                	row.sellprice = '0'
                }if(row.orignprice != '' && row.orignprice != '0'){
                	row.cType = '2';
                }
				if(row.cType == "2"){
					if(${office.grade == '50'}){
						row.sellprice = parseFloat(row.orignprice) + 20;
					}else{
					    row.sellprice = parseFloat(row.orignprice) + 10;
					}
				}
				$('#tab').bootstrapTable('updateByUniqueId', { id: row.batchnoId, 'row':row});
			}
			if (field !='cType'){
				setTimeout(function() {
					$('#tab').find('[data-uniqueid="' + row.batchnoId + '"]').next().find('[data-name="' + field + '"]').editable('show');
				},
				200);
			}
        }
	});
	
	$("#BtnAddBatchno").click(function() {
		var url = "${ctx }/vaccinate/bsVaccineBatchno/getList";
		var height = window.screen.availHeight * 0.6;
		var width = window.screen.availWidth * 0.7;
		layer.open({
			type : 2,
			title : '疫苗批次信息',
			shadeClose : true,
			shade : 0.8,
			resize : false,
			area : [ width + "px", height + "px" ],
			content : url
		});
	});
	
	$("#BtnDeleteBatchno").click(function() {
		var ids = $.map($('#tab').bootstrapTable('getSelections'), function (row) {
			return row.batchnoId;
        });
		$('#tab').bootstrapTable('remove', {
            field: 'batchnoId',
            values: ids
        });
	});
	//$("#name").focus();
	$("#inputForm").validate({
		submitHandler : function(form) {
			postOrderDetil(form);
		},
		ignore : "",
		errorContainer : "#messageBox",
		errorPlacement : function(error, element) {
			$("#messageBox").text("输入有误，请先更正。");
			if (element.is(":checkbox") || element.is(":radio")
					|| element.parent().is(".input-append")) {
				error.appendTo(element.parent().parent());
			} else {
				error.insertAfter(element);
			}
		}
	});
});


function getInfo(obj) {
	debugger;
	$('#tab').bootstrapTable('getData', false);
	var rows = $('#tab').bootstrapTable('getData');
	for(var i = 0 in rows){
		if(obj.batchnoId == rows[i].batchnoId){
			layer.msg("该批号已在出入库单中，请勿多次选择");
			return;
		}
	}
	var tr = {
		id : '',
		batchnoId : obj.batchnoId,
		batchType : obj.batchType,
		cType : obj.type,
		vaccineName : obj.vaccineName,
		companyName : obj.companyName,
		companyCode : obj.companyCode,
		batchno : obj.batchno,
		secondBatchno : obj.secondBatchno,
		spec : obj.spec,
		dose : obj.dose,
		vaccExpDate : obj.outBoundDate,
		storenum : ''
	}
	if (obj.type == 2){
		tr.orignprice = '';
		tr.sellprice = '';
	}else{
		tr.orignprice = 0;
		tr.sellprice = 0;
	}
	$('#tab').bootstrapTable('append', tr);
}

function postOrderDetil(form) {//表单保存时  将此方法中的数据尽行保存
	debugger;
	var rows = $('#tab').bootstrapTable('getData');
	var rowIndex = rows.length;
	if (rowIndex <= 0) {
		layer.msg("建账数据为空");
		return;
	}
	$("#tableData").val(JSON.stringify(rows));
	for(var i = 0;i<rowIndex;i++ ){
		var num = parseInt(rows[i].storenum);
		var num_reg = /^\+?[1-9][0-9]*$/;
		if (!num_reg.test(num) || num < 0 ) {
			layer.msg("建账数量不合法！");
			return;
		}
	}
	layer.confirm('确定保存建账？', {
		icon: 3, title:'提示',
		btn : [ '保存', '取消' ]
	}, function() {
		layer.load(2);
		loading('正在提交，请稍等...');
		form.submit();
	});
}

var specDic = jQuery.parseJSON('${fns:getDictListJson("syringe_spec")}');
var batchTypeDic = JSON.parse('${fns:getDictListJson("batch_type")}');
</script>
<style type="text/css">
input[type="checkbox"] {
	display: block;
}
.maxWidth{
	width: 210px;
}
.input-xlarge-search {
	width: 266px;
}
.inputType{
    background-color: rgb(255, 255, 255);
    box-shadow: rgba(0, 0, 0, 0.075) 0px 1px 1px inset;
    border-width: 1px;
    border-style: solid;
    border-color: rgb(204, 204, 204);
    border-image: initial;
    transition: border 0.2s linear, box-shadow 0.2s linear;
    border-radius: 4px;
    height: 10px;
    font-size: 14px;
    line-height: 10px;
    padding: 4px 6px;
}
</style>
</head>
<body>
	<ul class="nav nav-tabs">
		<li class="active"><a href="#">初期建账</a></li>
	</ul>
	<sys:message content="${message}"/>
	<form:form id="inputForm" modelAttribute="bsProduct" action="${ctx}/vaccinate/bsProduct/saveAdd" method="post"
		class="breadcrumb form-search">
		<input id="tableData" type="hidden" name="tableData">
		<ul class="ul-form">
			<li class="btns"><button id="BtnAddBatchno" class="btn btn-primary" type="button">选择批号</button></li>
			<li class="btns"><button id="BtnDeleteBatchno" class="btn btn-primary" type="button">删除批号</button></li>
			<li class="btns"><button id="BtnSave" class="btn btn-primary" type="submit" >保存</button></li>
		</ul>
	</form:form>
	<div class="control-group" style = "margin: 0px 10px;">
		<table id="tab">
		</table>
	</div>
</body>
</html>